//两数相加
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* head = new ListNode(-1);
        ListNode* tail = head;
        int t = 0;
        while (l1 || l2 || t) {
            int val = 0;
            if (l1) {
                val += l1->val;
                l1 = l1->next;
            }
            if (l2) {
                val += l2->val;
                l2 = l2->next;
            }
            if (t)
                val++;
            if (val >= 10) {
                val %= 10;
                t = 1;
            } else
                t = 0;
            tail->next = new ListNode(val);
            tail = tail->next;
        }
        return head->next;
    }
};

//环形链表 II
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* detectCycle(ListNode* head) {
        if (head == nullptr)
            return head;
        ListNode* f = head;
        ListNode* s = head;
        do {
            if (f) {
                f = f->next;
                if (f)
                    f = f->next;
            }
            s = s->next;
        } while (s != f);

        if (f == nullptr)
            return nullptr;

        ListNode* _s = head;
        while (_s != s) {
            _s = _s->next;
            s = s->next;
        }
        return s;
    }
};